﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO.IsolatedStorage;
using System.IO;
using System.Xml.Linq;
using Zeus.Classes;
using Zeus.Classes.Documents;
using Zeus.Classes.Events;
using Zeus.Events;
using System.Windows.Controls;
using System.Windows;
using Zeus.UserControls;


namespace Zeus.Classes
{
    public enum ProjectTypeEnum
    {
        TowDimentions,
        ThreeDimentions
    };

    public enum AIType
    {
        Action,
        Sports,
        Empty
    } ;

    static public class Project
    {
        static public List<Document> ProjectDocuments { set; get; }
        static public string ProjectName { set; get; }
        static public ProjectTypeEnum ProjectType { set; get; }
        static public AIType AItype{ set; get;}
        static private bool ProjectSave { set; get; }
        static public int ProjectCount { set; get; }
        //------------------------------------------------------- 
        static public void NewProject(string projectname, ProjectTypeEnum ProjType, AIType aiType)
        {
            ProjectName = projectname;
            ProjectType = ProjType;
            AItype = aiType;

            ProjectDocuments = new List<Document>();
            SceneDocument sceneDoc = new SceneDocument();
            sceneDoc.FileName = "MyGame";
            sceneDoc.FilePath = "./" + ProjectName + "/Scenes/MyGame";
            sceneDoc.Doc_ID = ProjectCount;
            ProjectCount++;
            ProjectDocuments.Add(sceneDoc);

            try
            {
                using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
                {


                    store.CreateDirectory(projectname);
                    string subdirectory = ("./" + projectname + "/App_Code");
                    store.CreateDirectory(subdirectory);
                    subdirectory = ("./" + projectname + "/Images");
                    store.CreateDirectory(subdirectory);
                    subdirectory = ("./" + projectname + "/Sounds");
                    store.CreateDirectory(subdirectory);
                    subdirectory = ("./" + projectname + "/Videos");
                    store.CreateDirectory(subdirectory);
                    subdirectory = ("./" + projectname + "/Other");
                    store.CreateDirectory(subdirectory);
                    subdirectory = ("./" + projectname + "/Meshes");
                    store.CreateDirectory(subdirectory);
                    subdirectory = ("./" + projectname + "/Scenes");
                    store.CreateDirectory(subdirectory);
                    subdirectory = ("./" + projectname + "/References");
                    store.CreateDirectory(subdirectory);
                    IsolatedStorageFileStream f = new IsolatedStorageFileStream(sceneDoc.FilePath, FileMode.CreateNew,
                                                                                store);
                    App.EventAggegator.GetEvent<UpDateSolution>().Publish(null);
                    AddClassProgram();
                    AddClassMyGame();
                    SaveAll();
                }
            }
            catch (IsolatedStorageException e)
            {
                string s = e.Message;
                // TODO: Handle that store was unable to be accessed.
            }
        }
        private static void AddClassProgram()
        {
            try
            {
                IsolatedStorageFile isolated = IsolatedStorageFile.GetUserStoreForApplication();
                XDocument documnet = XDocument.Load("class_references.xml");
                var Class = (from element in documnet.Root.Descendants() where element.Name == "classProgram" select element);
                using (IsolatedStorageFileStream strm = new IsolatedStorageFileStream("./" + ProjectName + "/App_Code/" + "Program.cs", FileMode.Create, isolated))
                {
                    using (StreamWriter sw = new StreamWriter(strm))
                    {
                        foreach (var x in Class)
                        {
                            string s = x.Attribute("name").Value.ToString();
                            s = s.Replace(";", ";" + Environment.NewLine);
                            s = s.Replace("{", "{" + Environment.NewLine);
                            s = s.Replace("}", "}" + Environment.NewLine + Environment.NewLine);
                            sw.Write(s);
                        }
                    }
                }

                TextDocument doc = new TextDocument(ProjectItem_Types.Class);
                doc.DocType = ProjectItem_Types.Class;
                doc.FileName = "Program.cs";
                doc.FilePath = "./" + ProjectName + "/App_Code/" + "Program.cs";
                doc.Doc_ID = ProjectCount;
                ProjectCount++;
                ProjectDocuments.Add(doc);
                ProjectSave = false;
                App.EventAggegator.GetEvent<UpDateSolution>().Publish(null);
            }
            catch (Exception e)
            {
            }
        }
        private static void AddClassMyGame()
        {
            try
            {
                IsolatedStorageFile isolated = IsolatedStorageFile.GetUserStoreForApplication();
                XDocument documnet = XDocument.Load("class_references.xml");
                var Class = (from element in documnet.Root.Descendants() where element.Name == "classMyGame" select element);
                using (IsolatedStorageFileStream strm = new IsolatedStorageFileStream("./" + ProjectName + "/App_Code/" + "MyGame.cs", FileMode.Create, isolated))
                {
                    using (StreamWriter sw = new StreamWriter(strm))
                    {
                        foreach (var x in Class)
                        {
                            string s = x.Attribute("name").Value.ToString();
                            s = s.Replace("Content.RootDirectory = ;", "Content.RootDirectory =" + "\"Content\"" + ";");
                            s = s.Replace(";", ";" + Environment.NewLine);
                            s = s.Replace("{", "{" + Environment.NewLine);
                            s = s.Replace("}", "}" + Environment.NewLine + Environment.NewLine);
                            sw.Write(s);
                        }
                    }
                }
                ProjectSave = false;
                TextDocument doc = new TextDocument(ProjectItem_Types.Class);
                doc.DocType = ProjectItem_Types.Class;
                doc.FileName = "MyGame.cs";
                doc.FilePath = "./" + ProjectName + "/App_Code/" + "MyGame.cs";
                doc.Doc_ID = ProjectCount;
                ProjectCount++;
                ProjectDocuments.Add(doc);
                App.EventAggegator.GetEvent<UpDateSolution>().Publish(null);
            }
            catch (Exception e)
            {
            }
        }
        static public void OpenProject(string pName)
        {
            try
            {
                ProjectDocuments = new List<Document>();
                ProjectSave = true;
                ProjectName = pName;
                using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    
                    using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("./" + ProjectName + "/" + ProjectName + ".xml", FileMode.Open, isoStore))
                    {
                        XDocument xdocument = XDocument.Load(isoStream);

                        if (xdocument.Root != null)
                        {
                            var elements = from element in xdocument.Root.Descendants()
                                           where element.Name == "Project"
                                           select element;

                            foreach (var x in elements)
                            {
                                ProjectName = x.Attribute("Name").Value;
                                //ProjectType = x.Attribute("Type").Value;
                            }

                            elements = from element in xdocument.Root.Descendants()
                                       where element.Name == "Item"
                                       select element;

                            foreach (var x in elements)
                            {
                                Document doc;
                                string type = x.Attribute("Extention").Value;
                                switch (type)
                                {
                                    case "Class":
                                        {
                                            doc = new TextDocument(ProjectItem_Types.Class);
                                            doc.FileName = x.Attribute("FileName").Value;
                                            doc.FilePath = x.Attribute("FilePath").Value;
                                            doc.Doc_ID = ProjectCount;
                                            ProjectDocuments.Add(doc);
                                            break;
                                        }
                                    case "Image":
                                        {
                                            doc = new ImageDocument();
                                            doc.FileName = x.Attribute("FileName").Value;
                                            doc.FilePath = x.Attribute("FilePath").Value;
                                            doc.Doc_ID = ProjectCount;
                                            ProjectDocuments.Add(doc);
                                            
                                            break;
                                        }
                                    case "Video":
                                        {
                                            doc = new videoDocument();
                                            doc.FileName = x.Attribute("FileName").Value;
                                            doc.FilePath = x.Attribute("FilePath").Value;
                                            doc.Doc_ID = ProjectCount;
                                            ProjectDocuments.Add(doc);
                                            break;
                                        }

                                    case "Mesh":
                                        {
                                            doc = new MeshDocument();
                                            doc.FileName = x.Attribute("FileName").Value;
                                            doc.FilePath = x.Attribute("FilePath").Value;
                                            doc.Doc_ID = ProjectCount;
                                            ProjectDocuments.Add(doc);
                                            break;
                                        }
                                    case "Text":
                                        {
                                            doc = new TextDocument(ProjectItem_Types.Text);
                                            doc.FileName = x.Attribute("FileName").Value;
                                            doc.FilePath = x.Attribute("FilePath").Value;
                                            doc.Doc_ID = ProjectCount;
                                            ProjectDocuments.Add(doc);
                                            break;
                                        }
                                    case "Scene":
                                        {
                                            doc = new SceneDocument();
                                            doc.FileName = x.Attribute("FileName").Value;
                                            doc.FilePath = x.Attribute("FilePath").Value;
                                            doc.Doc_ID = ProjectCount;
                                            ProjectDocuments.Add(doc);
                                            break;
                                        }
                                    case "Sound":
                                        {
                                            doc = new SoundDocument();
                                            doc.FileName = x.Attribute("FileName").Value;
                                            doc.FilePath = x.Attribute("FilePath").Value;
                                            doc.Doc_ID = ProjectCount;
                                            ProjectDocuments.Add(doc);
                                            break;
                                        }
                                    case "Reference":
                                        {
                                            doc = new OtherDocument();
                                            doc.FileName = x.Attribute("FileName").Value;
                                            doc.FilePath = x.Attribute("FilePath").Value;
                                            doc.Doc_ID = ProjectCount;
                                            ProjectDocuments.Add(doc);
                                            break;
                                        }

                                }
                                ProjectCount++;
                            }
                        }
                    }
                }
                App.EventAggegator.GetEvent<UpDateSolution>().Publish(null);
            }
            catch (Exception)
            {

                throw;
            }
        }
        static public List<string> IsSaved()
        {
            List<string> l = new List<string>();
            try
            {
                if (!ProjectSave)
                    l.Add(ProjectName);
                for (int i = 0; i < ProjectDocuments.Count; i++)
                {
                    if (ProjectDocuments[i].IsChanged)
                        l.Add(ProjectDocuments[i].FileName);
                }
            }
            catch (Exception)
            {

            }
            return l;
        }
        static public void SaveAll()
        {
            try
            {
                for (int i = 0; i < ProjectDocuments.Count; i++)
                {
                    if (ProjectDocuments[i].IsChanged == true)
                        ProjectDocuments[i].Save();
                }

                XElement[] lElement = new XElement[ProjectDocuments.Count + 1];

                lElement[0] = new XElement("Project", new XAttribute("Type", ProjectType), new XAttribute("Name", ProjectName));
                for (int i = 0; i < ProjectDocuments.Count; i++)
                {
                    lElement[i + 1] = new XElement("Item", new XAttribute("Extention", ProjectDocuments[i].DocType), new XAttribute("FileName", ProjectDocuments[i].FileName)
                        , new XAttribute("FilePath", ProjectDocuments[i].FilePath));
                }

                XDocument document = new XDocument(new XElement("Root", lElement));
                using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream isoStream =
                        new IsolatedStorageFileStream("./" + ProjectName + "/" + ProjectName + ".xml", FileMode.Create, isoStore))
                    {
                        document.Save(isoStream);
                    }
                }
                ProjectSave = true;
            }
            catch (Exception)
            {

            }
        }
        public static void CloseSolution()
        {
            ProjectName = "";
            ProjectDocuments.Clear();
            ProjectCount = 0;
            ProjectType = ProjectTypeEnum.TowDimentions;
            AItype = AIType.Action;
            App.EventAggegator.GetEvent<CloseSolution>().Publish(null);
            MessageBox.Show("Project Closed !!");

        }
        public static void DeleteProject()
        {
            IsolatedStorageFile isolated = IsolatedStorageFile.GetUserStoreForApplication();

            //for (int i = 0; i < ProjectDocuments.Count; i++)
            //{
            //    isolated.DeleteFile(ProjectDocuments[i].FilePath);
            //    ProjectDocuments.RemoveAt(i);
            //}
            string projectname = ProjectName;
            string subdirectory = ("" + projectname + "/App_Code");
            isolated.DeleteFile(subdirectory);
            subdirectory = ("./" + projectname + "/Images");
            isolated.DeleteFile(subdirectory);
            subdirectory = ("./" + projectname + "/Sounds");
            isolated.DeleteFile(subdirectory);
            subdirectory = ("./" + projectname + "/Videos");
            isolated.DeleteFile(subdirectory);
            subdirectory = ("./" + projectname + "/Other");
            isolated.DeleteFile(subdirectory);
            subdirectory = ("./" + projectname + "/Meshes");
            isolated.DeleteFile(subdirectory);
            subdirectory = ("./" + projectname + "/Scenes");
            isolated.DeleteFile(subdirectory);
            subdirectory = ("./" + projectname + "/References");
            isolated.DeleteFile(subdirectory);
            isolated.DeleteFile(projectname);
        }
        public static void DeleteFile(ListBox listBox)
        {
            for (int i = listBox.Items.Count - 1; i >= 0; i--)
            {
                ListBoxItem item = (ListBoxItem)(listBox.SelectedItems[i]);
                ProjectDocuments.RemoveAt(int.Parse(item.Tag.ToString()));
            }
            App.EventAggegator.GetEvent<UpDateSolution>().Publish(null);
            ProjectSave = false;
        }
        //--------------------------------------------------------
        public static long SpaceNeeded;
        public static bool Stor_IncreaseQuota(long sn)
        {
            try
            {
                using (IsolatedStorageFile Store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (Store.IncreaseQuotaTo(Store.Quota + sn) == true)
                    {
                        MessageBox.Show("Quota successfully increased. " + Store.AvailableFreeSpace + "" + Store.Quota);
                        return true;
                    }
                    else
                    {
                        MessageBox.Show("not increased");
                        return false;
                    }
                }

            }
            catch (IsolatedStorageException)
            {
                return false;
            }

        }
        private static void ClickOk(object sender, RoutedEventArgs e)
        {
            using (IsolatedStorageFile Store = IsolatedStorageFile.GetUserStoreForApplication())
            {

                if (true == Store.IncreaseQuotaTo(Store.Quota + SpaceNeeded))
                {
                    MessageBox.Show("Quota successfully increased. " + Store.AvailableFreeSpace + "" + Store.Quota);
                }
                else
                {
                    MessageBox.Show("not increased");
                }
            }


        }
        private static void ButtonNo_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Operation can't be completed.");
        }
        //--------------------------------------------------------
        public static void AddClass(string className)
        {
            try
            {
                IsolatedStorageFile isolated = IsolatedStorageFile.GetUserStoreForApplication();
                XDocument documnet = XDocument.Load("class_references.xml");
                var BeginClass = (from element in documnet.Root.Descendants() where element.Name == "data" select element);
                var EndClass = (from element in documnet.Root.Descendants() where element.Name == "EndClass" select element);
                using (IsolatedStorageFileStream strm = new IsolatedStorageFileStream("./" + ProjectName + "/App_Code/" + className, FileMode.Create, isolated))
                {
                    using (StreamWriter sw = new StreamWriter(strm))
                    {
                        foreach (var x in BeginClass)
                        {
                            string s = x.Attribute("name").Value.ToString();
                            sw.WriteLine(s);
                        }
                        sw.WriteLine("Public Class " +className);
                        foreach (var x in EndClass)
                        {
                            string s = x.Attribute("name").Value.ToString();
                            sw.WriteLine(s);
                        }
                    }
                }

                TextDocument doc = new TextDocument(ProjectItem_Types.Class);
                doc.DocType = ProjectItem_Types.Class;
                doc.FileName = className;
                doc.FilePath = "./" + ProjectName + "/App_Code/" + className;
                doc.Doc_ID = ProjectCount;
                ProjectCount++;
                ProjectDocuments.Add(doc);
                ProjectSave = false;
                App.EventAggegator.GetEvent<UpDateSolution>().Publish(null);
            }
            catch (Exception)
            {
               
            }
        }
        public static void AddText(string fileName)
        {
            try
            {
                IsolatedStorageFile isolated = IsolatedStorageFile.GetUserStoreForApplication();
                TextDocument doc = new TextDocument(ProjectItem_Types.Text);
                doc.FileName = fileName;
                doc.FilePath = "./" + ProjectName + "/Other/" + fileName;
                doc.Doc_ID = ProjectCount;
                ProjectCount++;
                ProjectDocuments.Add(doc);
                ProjectSave = false;
                isolated.CreateFile(doc.FilePath);
                App.EventAggegator.GetEvent<UpDateSolution>().Publish(null);
            }
            catch (Exception)
            {

            }
        }
        //-----------------------------------------------------------
        public static void AddClass()
        {
            OpenFileDialog OpenText = new OpenFileDialog();
            OpenText.Filter = "Text Files (*.cs;*.CS)|*.cs;*.CS| All Files (*.*)|*.*";
            OpenText.ShowDialog();
            IsolatedChildWindow ICW = new IsolatedChildWindow();
            IsolatedStorageFile isolated = IsolatedStorageFile.GetUserStoreForApplication();
            Stream fileStream = OpenText.File.OpenRead();
            IsolatedStorageFileStream iso = new IsolatedStorageFileStream("./" + ProjectName + "/Other/" + OpenText.File.Name, FileMode.Create, isolated);
            while (true)
            {
                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    SpaceNeeded = OpenText.File.Length;
                    long newSpace = store.Quota + SpaceNeeded;
                    if (SpaceNeeded > store.AvailableFreeSpace)
                    {
                        ICW.Show();
                        ICW.ButtonYes.Click += new RoutedEventHandler(ClickOk);
                        ICW.ButtonNo.Click += new RoutedEventHandler(ButtonNo_Click);
                    }
                }
                byte[] buffer = new byte[OpenText.File.Length];
                int count = fileStream.Read(buffer, 0, buffer.Length);

                if (count > 0)
                {
                    iso.Write(buffer, 0, count);
                }
                else
                {
                    break;
                }
            }
            iso.Flush();
            iso.Close();
            TextDocument doc = new TextDocument(ProjectItem_Types.Text);
            doc.FileName = OpenText.File.Name;
            doc.FilePath = "./" + ProjectName + "/Other/" + OpenText.File.Name;
            doc.Doc_ID = ProjectCount;
            ProjectCount++;
            ProjectDocuments.Add(doc);
            ProjectSave = false;
            App.EventAggegator.GetEvent<UpDateSolution>().Publish(null);
        }
        public static void AddReference()
        {
            OpenFileDialog openText = new OpenFileDialog();
            openText.Filter = "Library Files (*.dll;*.DLL)|*.dll;*.DLL";
            openText.ShowDialog();
            IsolatedChildWindow ICW = new IsolatedChildWindow();
            IsolatedStorageFile isolated = IsolatedStorageFile.GetUserStoreForApplication();
            Stream fileStream = openText.File.OpenRead();
            IsolatedStorageFileStream iso = new IsolatedStorageFileStream("./" + ProjectName + "/References/" + openText.File.Name, FileMode.Create, isolated);
            while (true)
            {
                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    SpaceNeeded = openText.File.Length;
                    if (SpaceNeeded > store.AvailableFreeSpace)
                    {
                        ICW.Show();
                        ICW.ButtonYes.Click += ClickOk;
                        ICW.ButtonNo.Click += ButtonNo_Click;
                    }
                }
                byte[] buffer = new byte[openText.File.Length];
                int count = fileStream.Read(buffer, 0, buffer.Length);

                if (count > 0)
                {
                    iso.Write(buffer, 0, count);
                }
                else
                {
                    break;
                }
            }
            iso.Flush();
            iso.Close();
            OtherDocument doc = new OtherDocument();
            doc.FileName = openText.File.Name;
            doc.FilePath += doc.FileName;
            doc.Doc_ID = ProjectCount;
            ProjectCount++;
            ProjectDocuments.Add(doc);
            ProjectSave = false;
            App.EventAggegator.GetEvent<UpDateSolution>().Publish(null);
        }
        public static void AddImage()
        {
            OpenFileDialog OpenImage = new OpenFileDialog();
            OpenImage.Filter = "JPEG Files (*.jpg;*.jpeg;*.GIF;*.PNG;*.BMP;*.CLP)|*.jpg;*.jpeg;*.GIF;*.PNG;*.BMP;*.CLP | All Files (*.*)|*.*";
            OpenImage.ShowDialog();
            IsolatedChildWindow ICW  = new IsolatedChildWindow();
            IsolatedStorageFile isolated = IsolatedStorageFile.GetUserStoreForApplication();
            Stream fileStream = OpenImage.File.OpenRead();
            IsolatedStorageFileStream iso = new IsolatedStorageFileStream("./" + ProjectName + "/Images/" + OpenImage.File.Name, FileMode.Create, isolated);
            while (true)
            {
                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    SpaceNeeded = OpenImage.File.Length;
                    long newSpace = store.Quota + SpaceNeeded;
                    if (SpaceNeeded > store.AvailableFreeSpace)
                    {
                        ICW.Show();
                        ICW.ButtonYes.Click += new RoutedEventHandler(ClickOk);
                        ICW.ButtonNo.Click += new RoutedEventHandler(ButtonNo_Click);
                    }
                }
                byte[] buffer = new byte[OpenImage.File.Length];
                int count = fileStream.Read(buffer, 0, buffer.Length);

                if (count > 0)
                {
                    iso.Write(buffer, 0, count);
                }
                else
                {
                    break;
                }
            }
            iso.Flush();
            iso.Close();

            ImageDocument doc = new ImageDocument();
            doc.DocType = ProjectItem_Types.Image;
            doc.FileName = OpenImage.File.Name;
            doc.FilePath = "./" + ProjectName + "/Images/" + OpenImage.File.Name;
            doc.Doc_ID = ProjectCount;
            ProjectCount++;
            ProjectDocuments.Add(doc);
            ProjectSave = false;
            App.EventAggegator.GetEvent<UpDateSolution>().Publish(null);
        }
        public static void AddSound()
        {
            try
            {
                OpenFileDialog OpenSound = new OpenFileDialog();
                OpenSound.Filter = "Audio Files (*.wav;*.LBCM;*.MP3;*.AAC)|*.wav;*.LBCM;*.MP3;*.AAC| All Files (*.*)|*.*";
                OpenSound.ShowDialog();
                IsolatedChildWindow ICW = new IsolatedChildWindow();
                IsolatedStorageFile isolated = IsolatedStorageFile.GetUserStoreForApplication();
                Stream fileStream = OpenSound.File.OpenRead();
                IsolatedStorageFileStream iso = new IsolatedStorageFileStream("./" + ProjectName + "/Sounds/" + OpenSound.File.Name, FileMode.Create, isolated);
                while (true)
                {
                    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        SpaceNeeded = OpenSound.File.Length;
                        long newSpace = store.Quota + SpaceNeeded;
                        if (SpaceNeeded > store.AvailableFreeSpace)
                        {
                            ICW.Show();
                            ICW.ButtonYes.Click += new RoutedEventHandler(ClickOk);
                            ICW.ButtonNo.Click += new RoutedEventHandler(ButtonNo_Click);
                        }
                    }
                    byte[] buffer = new byte[OpenSound.File.Length];
                    int count = fileStream.Read(buffer, 0, buffer.Length);
                    if (count > 0)
                    {
                        iso.Write(buffer, 0, count);
                    }
                    else
                    {
                        break;
                    }
                }
                iso.Flush();
                iso.Close();


                SoundDocument doc = new SoundDocument();
                doc.DocType = ProjectItem_Types.Sound;
                doc.FileName = OpenSound.File.Name;
                doc.FilePath = "./" + ProjectName + "/Sounds/" + OpenSound.File.Name;
                doc.Doc_ID = ProjectCount;
                ProjectCount++;
                ProjectDocuments.Add(doc);
                ProjectSave = false;
                App.EventAggegator.GetEvent<UpDateSolution>().Publish(null);
            }

            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
        public static void AddVideo()
        {
            try
            {
                OpenFileDialog OpenVideo = new OpenFileDialog();
                OpenVideo.Filter = "Video File(*.avi;*.wmv;*.wmva;*.RGBA;*.WVC1;*.H264)| *.avi;*.wmv;*.wmva;*.RGBA;*.WVC1;*.H264 | All Files (*.*)|*.*";
                OpenVideo.ShowDialog();
                IsolatedChildWindow ICW = new IsolatedChildWindow();
                IsolatedStorageFile isolated = IsolatedStorageFile.GetUserStoreForApplication();
                IsolatedStorageFileStream iso = new IsolatedStorageFileStream("./" + ProjectName + "/Videos/" + OpenVideo.File.Name, FileMode.OpenOrCreate, isolated);
                Stream filestream = OpenVideo.File.OpenRead();

                while (true)
                {
                    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        SpaceNeeded = OpenVideo.File.Length;
                        long newSpace = store.Quota + SpaceNeeded;
                        if (SpaceNeeded > store.AvailableFreeSpace)
                        {
                            ICW.Show();
                            ICW.ButtonYes.Click += new RoutedEventHandler(ClickOk);
                            ICW.ButtonNo.Click += new RoutedEventHandler(ButtonNo_Click);
                        }
                    }
                    byte[] buffer = new byte[filestream.Length];
                    int count = filestream.Read(buffer, 0, buffer.Length);
                    
                    if (count > 0)
                    {
                        iso.Write(buffer,0,count);
                    }
                    else
                    {
                        break;
                    }
                }
                iso.Flush();
                iso.Close();

                videoDocument doc = new videoDocument();
                doc.DocType = ProjectItem_Types.Video;
                doc.FileName = OpenVideo.File.Name;
                doc.FilePath = "./" + ProjectName + "/Videos/" + OpenVideo.File.Name;
                
                doc.Doc_ID = ProjectCount;
                ProjectCount++;
                ProjectDocuments.Add(doc);
                ProjectSave = false;
                App.EventAggegator.GetEvent<UpDateSolution>().Publish(null);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
    }
}
